<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/8/25
 * Time: 11:14
 */

namespace app\admin\controller;

use think\Db;

class Statuschange extends Base
{
    /**
     * 用户启用/冻结操作记录
     */
    public function index(){
        if(request()->isAjax ()){
            $type = input('type'); // 1 登录   2 团队长   3 省代   4 市代  5 县代
            $status = input('status'); // 1 启用   2 冻结
            $real_name = input('real_name');

            $where = [];
            if(isset($real_name) && $real_name != ''){
                $where['uuu.user_real_name'] = ['LIKE', '%' . $real_name . '%'];
            }
            if(isset($type) && $type != ''){
                $where['u.type'] = $type;
            }
            if(isset($status) && $status != ''){
                $where['u.status'] = $status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_use_freeze')
                ->alias('u')
                ->join('up_u_user uuu', 'u.user_id = uuu.user_id', 'LEFT')
                ->where($where)
                ->count('u.id');
            $lists = Db::table('up_use_freeze')
                ->alias('u')
                ->join('up_u_user uuu', 'u.user_id = uuu.user_id', 'LEFT')
                ->field('u.id, uuu.user_real_name, u.type, u.status, u.remark, admin, u.time')
                ->where($where)
                ->order('u.id desc')
                ->page($Nowpage,$limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $ids = input('ids');

        $where = [];
        if($ids == -1){
            $type = input('type'); // 1 登录   2 团队长   3 省代   4 市代  5 县代
            $status = input('status'); // 1 启用   2 冻结
            $real_name = input('real_name');

            if(isset($real_name) && $real_name != ''){
                $where['uuu.user_real_name'] = ['LIKE', '%' . $real_name . '%'];
            }
            if(isset($type) && $type != ''){
                $where['u.type'] = $type;
            }
            if(isset($status) && $status != ''){
                $where['u.status'] = $status;
            }
        }else{
            if($ids == '') return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            $where['u.id'] = ['IN', $ids];
        }

        $lists = Db::table('up_use_freeze')
            ->alias('u')
            ->join('up_u_user uuu', 'u.user_id = uuu.user_id', 'LEFT')
            ->field('u.id, uuu.user_real_name, u.type, u.status, u.remark, admin, u.time')
            ->where($where)
            ->order('u.id desc')
            ->select();

        foreach($lists as $k => $v){
            // 操作身份
            switch ($v['type']){
                case 1: $lists[$k]['type'] = '登录';
                    break;
                case 2: $lists[$k]['type'] = '团队长';
                    break;
                case 3: $lists[$k]['type'] = '省代';
                    break;
                case 4: $lists[$k]['type'] = '市代';
                    break;
                case 5: $lists[$k]['type'] = '县代';
                    break;
                default:$lists[$k]['type'] = '未知';
            }
            // 状态
            $lists[$k]['status'] = $v['status'] == 1 ? '启用' : '冻结';
        }
        $cellname = [
            ['user_real_name', '操作对象', 10, 'LEFT'],
            ['type', '操作身份', 20, 'LEFT'],
            ['status', '操作状态', 25, 'LEFT'],
            ['remark', '备注', 20, 'LEFT'],
            ['time', '操作时间', 20, 'LEFT'],
            ['admin', '操作人员', 20, 'LEFT']
        ];
        $res = exportExcelZhy('用户状态', 'userStatus', $cellname, $lists);
        return json($res);
    }
}